﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace community
{
    public partial class children : Form
    {
        SqlConnection cn;
        SqlCommand cm;
        SqlDataAdapter dt;
        DataSet ds;
        SqlDataReader dr;
        string child_JMBH;
        string FMFS="";//分娩方式
        int state = 1;
        public static string childstate = "child";
        public children()
        {
            InitializeComponent();
        }

        private void children_Load(object sender, EventArgs e)
        {

        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            childcheck rc = new childcheck();
            rc.ShowDialog();
            child_JMBH =childcheck.child_JMBH;
            clearchild();
            clearchildbirth();
            getchild(child_JMBH);
            getchildbirth(child_JMBH);
        }
        private void getchild(string JMBH)
        {
            string str = "select inhabitantcode_chr as 儿童编号,residentname_chr as 儿童姓名,communityname_chr as 所属社区,identitycard_chr as 身份证号,birth_dat as 出生日期,sex_chr as 儿童性别 from T_CS_INHABITANTRECORD_NEW where inhabitantcode_chr='" + JMBH + "'"; ;
            cn = new SqlConnection(SQL.conStr);
            cn.Open();
            cm = new SqlCommand(str,cn);
            dr= cm.ExecuteReader();
            if(dr.Read())
            {
                textBox1.Text=dr["儿童姓名"].ToString();
                textBox2.Text=dr["儿童编号"].ToString();
                textBox3.Text=dr["所属社区"].ToString();
                textBox4.Text = dr["身份证号"].ToString();
                textBox5.Text = dr["儿童性别"].ToString();
                dateTimePicker1.Text= dr["出生日期"].ToString();
                 
            }
            cn.Close();
        }
        private void getchildbirth(string JMBH)
        {
            string sql = "select INHABITANTCODE_VCHR as 儿童编号, HKCOUNTY_VCHR as 区 , HKTOWN_VCHR as 街,HKVILLAGE_VCHR as 居委,"
            + "NURSERYGARDENNAME_VCHR as 入托园名称,BIRTHHOSPITAL_VCHR as 出生医院, FATHERNAME_VCHR as 父亲姓名,FATHERAGE_INT as 父亲年龄,MOTHERNAME_VCHR as 母亲姓名,"
                  + " MOTHERAGE_INT as 母亲年龄,GRAVIDITY_INT as 孕次 ,PARITY_INT as 产次, GESTATIONALWEEKS_INT as 孕周, DELIVERYMETHOD_CHR as 分娩方式,"
            +"BIRHWEIGHT_INT as 出生体重, BIRHHEIGHT_INT as 出生身长, BIRHHIGHRISKFACTOR1_INT as 出生高危因素1,  BIRHHIGHRISKFACTOR2_INT as 出生高危因素2,  BIRHHIGHRISKFACTOR3_INT as 出生高危因素3,"
                  + "BIRHHIGHRISKFACTOR4_INT as 出生高危因素4,  BIRHHIGHRISKFACTOR5_INT as 出生高危因素5,  BIRHHIGHRISKFACTOR6_INT as 出生高危因素6, "
            +"BIRHHIGHRISKFACTOR7_INT as 出生高危因素7,  BIRHHIGHRISKFACTOR8_INT as 出生高危因素8, BIRHHIGHRISKFACTOR9_INT as 出生高危因素9,  LEFTEAROAE_VCHR as 左耳OAE,"
                  + "LEFTEARAABR_VCHR as 左耳AABR,RIGHTEAROAE_VCHR as 右耳OAE,RIGHTEARAABR_VCHR as 右耳AABR,NEONATALDISEASE_CHR as 新生儿期疾病,"
            +"NEONATALDISEASEQK_VCHR as 新生儿期疾病情况,MIDWIFERYHOSPITAL_VCHR as 接产医院  from T_CS_CHILDRENBIRTHINFO where INHABITANTCODE_VCHR='"+JMBH+"'";
            cn = new SqlConnection(SQL.conStr);
            cn.Open();
            cm = new SqlCommand(sql,cn);
            dr = cm.ExecuteReader();
            if (dr.Read())
            {
                textBox6.Text = dr["区"].ToString(); ;
                textBox7.Text = dr["街"].ToString();
                textBox8.Text = dr["居委"].ToString();
                textBox9.Text = dr["入托园名称"].ToString();
                textBox10.Text = dr["出生医院"].ToString();
                textBox11.Text = dr["父亲姓名"].ToString();
                textBox12.Text = dr["父亲年龄"].ToString();
                textBox13.Text = dr["母亲姓名"].ToString();
                textBox14.Text = dr["母亲年龄"].ToString();
                textBox15.Text = dr["孕次"].ToString();
                textBox16.Text = dr["产次"].ToString();
                textBox17.Text = dr["孕周"].ToString();
                if (dr["孕周"].ToString() == "顺产") radioButton1.Checked = true;
                if (dr["孕周"].ToString() == "吸引产") radioButton2.Checked = true;
                if (dr["孕周"].ToString() == "钳产") radioButton3.Checked = true;
                if (dr["孕周"].ToString() == "剖宫产") radioButton4.Checked = true;
                if (dr["孕周"].ToString() == "助产") radioButton5.Checked = true;
                if (dr["孕周"].ToString() == "臀牵引") radioButton6.Checked = true;

                textBox18.Text = dr["出生体重"].ToString();
                textBox19.Text = dr["出生身长"].ToString();
                if (dr["出生高危因素1"].ToString() == "1") checkBox1.Checked = true; else checkBox1.Checked = false;
                if (dr["出生高危因素2"].ToString() == "1") checkBox2.Checked = true; else checkBox2.Checked = false;
                if (dr["出生高危因素3"].ToString() == "1") checkBox3.Checked = true; else checkBox3.Checked = false;
                if (dr["出生高危因素4"].ToString() == "1") checkBox4.Checked = true; else checkBox4.Checked = false;
                if (dr["出生高危因素5"].ToString() == "1") checkBox5.Checked = true; else checkBox5.Checked = false;
                if (dr["出生高危因素6"].ToString() == "1") checkBox6.Checked = true; else checkBox6.Checked = false;
                if (dr["出生高危因素7"].ToString() == "1") checkBox7.Checked = true; else checkBox7.Checked = false;
                if (dr["出生高危因素8"].ToString() == "1") checkBox8.Checked = true; else checkBox8.Checked = false;
                if (dr["出生高危因素9"].ToString() == "1") checkBox9.Checked = true; else checkBox9.Checked = false;
                textBox20.Text = dr["左耳OAE"].ToString();
                textBox21.Text = dr["左耳AABR"].ToString();
                textBox22.Text = dr["右耳OAE"].ToString();
                textBox23.Text = dr["右耳AABR"].ToString();
                if (dr["新生儿期疾病"].ToString() == "无") radioButton7.Checked = true;
                if (dr["新生儿期疾病"].ToString() == "有") radioButton8.Checked = true;
                textBox24.Text = dr["新生儿期疾病情况"].ToString();
                textBox25.Text = dr["接产医院"].ToString();
            }
            cn.Close();
        }
        private void newchildbirth(string JMBH)
        {
            int i1;
            int i2;
            int i3;
            int i4;
            int i5;
            int i6;
            int i7;
            int i8;
            int i9;
            string st="";
            if (checkBox1.Checked)  i1=1;  else i1=0;
            if (checkBox2.Checked) i2 = 1; else i2 = 0;
            if (checkBox3.Checked) i3 = 1; else i3 = 0;
            if (checkBox4.Checked) i4 = 1; else i4 = 0;
            if (checkBox5.Checked) i5 = 1; else i5 = 0;
            if (checkBox6.Checked) i6 = 1; else i6 = 0;
            if (checkBox7.Checked) i7 = 1; else i7 = 0;
            if (checkBox8.Checked) i8 = 1; else i8 = 0;
            if (checkBox9.Checked) i9 = 1; else i9 = 0;

            if (radioButton7.Checked)
                st="无";
            else
                st="";
              if (radioButton8.Checked)
                st="有";
            else
                st="";
            string str="";
            have();
            if (state ==1)
            str = "insert into  T_CS_CHILDRENBIRTHINFO (INHABITANTCODE_VCHR, HKCOUNTY_VCHR , HKTOWN_VCHR,HKVILLAGE_VCHR,NURSERYGARDENNAME_VCHR,BIRTHHOSPITAL_VCHR, FATHERNAME_VCHR,FATHERAGE_INT,MOTHERNAME_VCHR,"
                  + " MOTHERAGE_INT,GRAVIDITY_INT,PARITY_INT, GESTATIONALWEEKS_INT, DELIVERYMETHOD_CHR, BIRHWEIGHT_INT, BIRHHEIGHT_INT, BIRHHIGHRISKFACTOR1_INT,  BIRHHIGHRISKFACTOR2_INT,  BIRHHIGHRISKFACTOR3_INT,"
                  + "BIRHHIGHRISKFACTOR4_INT,  BIRHHIGHRISKFACTOR5_INT,  BIRHHIGHRISKFACTOR6_INT,  BIRHHIGHRISKFACTOR7_INT,  BIRHHIGHRISKFACTOR8_INT, BIRHHIGHRISKFACTOR9_INT,  LEFTEAROAE_VCHR,"
                  + "LEFTEARAABR_VCHR,RIGHTEAROAE_VCHR,RIGHTEARAABR_VCHR,NEONATALDISEASE_CHR,NEONATALDISEASEQK_VCHR,MIDWIFERYHOSPITAL_VCHR) values('" + JMBH + "','" + textBox6.Text.Trim() + "','" + textBox7.Text.Trim() + "','"
                  + textBox8.Text.Trim() + "','" + textBox9.Text.Trim() + "','" + textBox10.Text.Trim() + "','" + textBox11.Text.Trim() + "'," + textBox12.Text.Trim() + ",'" + textBox13.Text.Trim() + "'," + textBox14.Text.Trim() + ","
                  + textBox15.Text.Trim() + "," + textBox16.Text.Trim() + "," + textBox17.Text.Trim() + ",'" + FMFS + "'," + textBox18.Text.Trim() + "," + textBox19.Text.Trim() + "," + i1 + "," + i2 + "," + i3 + "," + i4 + "," + i5 + "," + i6 + "," + i7 + ","
                  + i8 + "," + i9 + ",'" + textBox20.Text.Trim() + "','" + textBox21.Text.Trim() + "','" + textBox22.Text.Trim() + "','" + textBox23.Text.Trim() + "','" + st + "','" + textBox24.Text.Trim() + "','" + textBox25.Text.Trim() + "')";
            else
                str = "update T_CS_CHILDRENBIRTHINFO set INHABITANTCODE_VCHR='" + JMBH + "', HKCOUNTY_VCHR='" + textBox6.Text.Trim() + "', HKTOWN_VCHR='" + textBox7.Text.Trim() + "',HKVILLAGE_VCHR='" + textBox8.Text.Trim()
                    + "',NURSERYGARDENNAME_VCHR='" + textBox9.Text.Trim() + "',BIRTHHOSPITAL_VCHR='" + textBox10.Text.Trim() + "', FATHERNAME_VCHR='" + textBox11.Text.Trim() + "',FATHERAGE_INT=" + textBox12.Text.Trim() + ",MOTHERNAME_VCHR='"
                    + textBox13.Text.Trim() + "', MOTHERAGE_INT=" + textBox14.Text.Trim() + ",GRAVIDITY_INT=" + textBox15.Text.Trim() + ",PARITY_INT=" + textBox16.Text.Trim() + ",GESTATIONALWEEKS_INT=" + textBox17.Text.Trim() + ", DELIVERYMETHOD_CHR='" + FMFS
                    + "', BIRHWEIGHT_INT=" + textBox18.Text.Trim() + ", BIRHHEIGHT_INT=" + textBox19.Text.Trim() + ", BIRHHIGHRISKFACTOR1_INT=" + i1 + ",BIRHHIGHRISKFACTOR2_INT=" + i2 + ",BIRHHIGHRISKFACTOR3_INT=" + i3
                  + ",BIRHHIGHRISKFACTOR4_INT=" + i4 + ",BIRHHIGHRISKFACTOR5_INT=" + i5 + ",BIRHHIGHRISKFACTOR6_INT=" + i6 + ",BIRHHIGHRISKFACTOR7_INT=" + i7 + ",BIRHHIGHRISKFACTOR8_INT=" + i8 + ",BIRHHIGHRISKFACTOR9_INT=" + i9
                  + ",LEFTEAROAE_VCHR='" + textBox20.Text.Trim() + "',LEFTEARAABR_VCHR='" + textBox21.Text.Trim() + "',RIGHTEAROAE_VCHR='" + textBox22.Text.Trim() + "',RIGHTEARAABR_VCHR='" + textBox23.Text.Trim()
                  + "',NEONATALDISEASE_CHR='" + st + "',NEONATALDISEASEQK_VCHR='" + textBox24.Text.Trim() + "',MIDWIFERYHOSPITAL_VCHR='" + textBox25.Text.Trim() + "'  where INHABITANTCODE_VCHR='" + JMBH + "'";
            if (state!=1 && state!=0)
            {
                MessageBox.Show("异常");
                return;
            }
            cn = new SqlConnection(SQL.conStr);
            cm = new SqlCommand(str,cn);
            cn.Open();
            cm.ExecuteNonQuery();
            cn.Close();
            if (state == 1)
                MessageBox.Show("新增成功");
            else
                MessageBox.Show("修改成功");
        }

        private void have()//判断这个儿童有没有出生情况信息，有，state=0；没有，state1
        {
            string str1 = "select count(*) from T_CS_CHILDRENBIRTHINFO where INHABITANTCODE_VCHR='" + textBox2.Text.Trim() + "'";
            cn = new SqlConnection(SQL.conStr);
            cm = new SqlCommand(str1,cn);
            cn.Open();
            if (Convert.ToInt16(cm.ExecuteScalar()) == 0)
                state = 1;
            else
                state = 0;
            cn.Close();
        }

        private void clearchild()
        {
            textBox1.Text ="" ;
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text ="";
            textBox5.Text ="";
            dateTimePicker1.Text = "";
        }
        private void clearchildbirth()
        {
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = ""; 
            textBox13.Text = ""; 
            textBox14.Text = "";
            textBox15.Text = "";
            textBox16.Text = "";
            textBox17.Text = "";
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
            radioButton4.Checked = false;
            radioButton5.Checked = false;
            radioButton6.Checked = false;
            textBox18.Text = "";
            textBox19.Text = "";
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            checkBox5.Checked = false;
            checkBox6.Checked = false;
            checkBox7.Checked = false;
            checkBox8.Checked = false;
            checkBox9.Checked = false;
            textBox20.Text = "";
            textBox21.Text = "";
            textBox22.Text = "";
            textBox23.Text = "";
            radioButton7.Checked = false;
            radioButton8.Checked = false;
            textBox24.Text = "";
            textBox25.Text = "";

        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton7.Checked)
            {
                textBox24.Text = "";
                textBox24.Enabled = false;
            }
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton8.Checked)
            {
                textBox24.Enabled = true;
                textBox24.Focus();
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if(radioButton1.Checked==true)
                FMFS="顺产";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
                FMFS = "吸引产";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked == true)
                FMFS = "钳产";
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked == true)
                FMFS = "剖宫产";
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked == true)
                FMFS = "助产";
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton6.Checked == true)
                FMFS = "臀牵引";
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请添加儿童姓名");
                return;
            }
            state = 1;
            clearchildbirth();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
           newchildbirth(child_JMBH);
           
        }

        private void updatechildbirth(string str)
        {
 
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)//删除
        {
            if(textBox1.Text=="")
            {
                MessageBox.Show("请选择儿童信息");
                return;
                
            }
            string str = "select count(*) from T_CS_CHILDRENBIRTHINFO where INHABITANTCODE_VCHR='" + textBox2.Text.Trim() + "'";
            string sql = "delete from T_CS_CHILDRENBIRTHINFO where INHABITANTCODE_VCHR='" + textBox2.Text.Trim() + "'";
            cn = new SqlConnection(SQL.conStr);
            SqlCommand com1 = new SqlCommand(str,cn);
            SqlCommand com2 = new SqlCommand(sql,cn);
            cn.Open();
            if (Convert.ToInt16( com1.ExecuteScalar()) == 0)
            {
                MessageBox.Show("该居民没有记录，删除不成功");
                cn.Close();
                return;
            }
            if (MessageBox.Show("确定删除家庭信息?", "提示", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;
            com2.ExecuteNonQuery();
            cn.Close();
            MessageBox.Show("删除成功");
            clearchildbirth();
        }

       
    }
}
